package Eclass;

import ibatis.IbatisHelper;

import java.util.*;
import java.util.Date;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;

import Eclass.Exam.ExamModel;

import com.ibatis.sqlmap.client.SqlMapClient;

public class EclassManager {
	private SqlMapClient ibatis;

	public EclassManager() {
		this.ibatis = IbatisHelper.getSqlMapInstance();
	}

	public List selectAll(String userid) {
		List result = null;
		
		try {
			Map map = new HashMap();
			map.put("userid", userid);
			
			result = ibatis.queryForList("eClass.getUser2",map);
			
			System.out.println("\n\nPanjang List : " + result.size());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	public boolean resetPassword(EclassModel eclassModel) {
		boolean result = true; 
		try {
			ibatis.startTransaction();
//			
			System.out.println(eclassModel.getUsrupd() + "\n\n");
			System.out.println(eclassModel.getUserid() + "\n\n");
			
			eclassModel.setPassword("password");
			
			// default password : adins-ace
//			eclassModel.setPassword(sha1("adminadmin"));
			
//			System.out.println(eclassModel.getPassword() + "\n\n");
//			
//			System.out.println(eclassModel.getPassword().length() + "\n\n");
			
			this.ibatis.insert("eClass.resetPassword", eclassModel);
			ibatis.commitTransaction();
			
			System.out.println("Password sudah direset\n\n");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result = false;
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		return result;
	}
	
	public boolean updateUser(EclassModel eclassModel) {
		boolean result = true; 
		try {
			ibatis.startTransaction();
			
			System.out.println(eclassModel.getActive() + "\n\n");
			System.out.println(eclassModel.getIsloggedin() + "\n\n");
			System.out.println(eclassModel.getUsrupd() + "\n\n");
			System.out.println(eclassModel.getUserid() + "\n\n");			
			
			if ("candidate".equalsIgnoreCase(eclassModel.getRole()) || "admin".equalsIgnoreCase(eclassModel.getRole())) {
				this.ibatis.insert("eClass.updateNewUser1", eclassModel);
			} else if ("trainee".equalsIgnoreCase(eclassModel.getRole()) || "trainer".equalsIgnoreCase(eclassModel.getRole())) {
				this.ibatis.insert("eClass.updateOldUser1", eclassModel);
			}
			
			ibatis.commitTransaction();
			
			System.out.println("berhasil update\n\n");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result = false;
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		return result;
	}
	
	public String sha1(String input) throws NoSuchAlgorithmException {
        MessageDigest mDigest = MessageDigest.getInstance("SHA1");
        byte[] result = mDigest.digest(input.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < result.length; i++) {
            sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
        }
         
        return sb.toString();
    }
}